package com.yanggeng.demo.beginnerguidedemo.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.activity.BaseActivity;
import com.yanggeng.demo.beginnerguidedemo.bean.PayTypeBean;
import com.yanggeng.demo.beginnerguidedemo.utils.SnackbarUtil;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 网格单选recycleView
 * Created by yanggeng on 2017/7/3.
 */

public class GridRecycleViewActivity extends BaseActivity implements View.OnClickListener {

    private RecyclerView recyclerView;
    private GridLayoutManager gridLayoutManager;
    private View view;
    private PayTypeAdapter adapter;
    private List<PayTypeBean> datas;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_recycleview);
        setTitle(true, "请选择消费类型");
        recyclerView = (RecyclerView) findViewById(R.id.rv_pay);
        findViewById(R.id.btn_submit).setOnClickListener(this);
        view = findViewById(R.id.grid_recycleview);
        gridLayoutManager = new GridLayoutManager(this, 4, OrientationHelper.VERTICAL, false);
        recyclerView.setLayoutManager(gridLayoutManager);

        datas = new ArrayList<>();
        datas.add(new PayTypeBean(R.mipmap.pay_type, false, "加油"));
        datas.add(new PayTypeBean(R.mipmap.pay_type, false, "停车"));
        datas.add(new PayTypeBean(R.mipmap.pay_type, false, "洗车"));
        datas.add(new PayTypeBean(R.mipmap.pay_type, false, "保养"));
        datas.add(new PayTypeBean(R.mipmap.pay_type, false, "维修"));
        datas.add(new PayTypeBean(R.mipmap.pay_type, false, "违章"));
        datas.add(new PayTypeBean(R.mipmap.pay_type, false, "过路"));
        datas.add(new PayTypeBean(R.mipmap.pay_type, false, "其他"));

        adapter = new PayTypeAdapter(this, datas);
        adapter.check(0);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.btn_submit:
                SnackbarUtil.longSnackbar(view, "选中position : " + adapter.getCheckedPosition() + ", 内容 : " + datas.get(adapter.getCheckedPosition()).toString());
                break;
        }
    }

    class PayTypeAdapter extends RecyclerView.Adapter<PayTypeAdapter.PayTypeViewHolder> {

        private List<PayTypeBean> datas;
        private LayoutInflater inflater;
        private OnItemClickListener onItemClickListener;
        private SparseBooleanArray mSelectArray;
        private int checkedPosition;

        public PayTypeAdapter(Context context, List<PayTypeBean> datas) {
            inflater = LayoutInflater.from(context);
            this.datas = datas;
            mSelectArray = new SparseBooleanArray();
            checkedPosition = 0;
        }

        @Override
        public PayTypeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new PayTypeViewHolder(inflater.inflate(R.layout.layout_pay_type_item, parent, false));
        }

        @Override
        public void onBindViewHolder(final PayTypeViewHolder holder, final int position) {
            PayTypeBean payTypeBean = datas.get(position);
            holder.tv_pay_type.setText(payTypeBean.getPayType());
            holder.iv_pay_type.setImageResource(payTypeBean.getImageResId());
            if(mSelectArray.get(position, false)) {
                holder.iv_checked.setVisibility(View.VISIBLE);
            } else {
                holder.iv_checked.setVisibility(View.GONE);
            }
            holder.view_pay_type.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(holder.iv_checked.getVisibility() == View.GONE) {
                        mSelectArray.clear();
                        holder.iv_checked.setVisibility(View.VISIBLE);
                        mSelectArray.put(position, true);
                        checkedPosition = position;
                        notifyDataSetChanged();
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return datas.size();
        }

        public void check(int position) {
            mSelectArray.put(position, true);
            notifyDataSetChanged();
        }

        public int getCheckedPosition() {
            return checkedPosition;
        }

        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.onItemClickListener = onItemClickListener;
        }

        class PayTypeViewHolder extends RecyclerView.ViewHolder {

            ImageView iv_pay_type;
            ImageView iv_checked;
            TextView tv_pay_type;
            View view_pay_type;
            public PayTypeViewHolder(View itemView) {
                super(itemView);
                iv_pay_type = (ImageView) itemView.findViewById(R.id.iv_pay);
                iv_checked = (ImageView) itemView.findViewById(R.id.iv_checked);
                tv_pay_type = (TextView) itemView.findViewById(R.id.tv_pay_type);
                view_pay_type = itemView.findViewById(R.id.view_pay_type);
            }

            public void check() {
                if(iv_checked.getVisibility() == View.GONE) {
                    iv_checked.setVisibility(View.VISIBLE);
                } else {
                    iv_checked.setVisibility(View.GONE);
                }
            }
        }

    }

    public interface OnItemClickListener {
        public void onItemClick(View view, int position, long id);
    }
}
